<template>
  <div class="user_box">
    <!-- 顶部搜索 -->
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item>
        <el-input
          v-model="formInline.userInput"
          placeholder="根据用户名搜索"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="inputClick">清空</el-button>
        <el-button type="primary" @click="onSerch">搜索</el-button>
      </el-form-item>

      <!-- 新增user -->
      <el-button class="addBtn" type="primary" icon="el-icon-edit" @click="dialogBtn('create')">新增用户</el-button>
      <!-- 新增的表单 -->
      <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible">
      <!-- <el-dialog v-if="dialogType === 'editUser'" title="编辑用户" :visible.sync="dialogFormVisible"> -->

        <el-form :model="form" :rules="rules" label-width="100px">
          <el-form-item label="用户名" :label-width="formLabelWidth" prop="username">
            <el-input v-model="form.username"></el-input>
          </el-form-item>
          <el-form-item label="邮箱" :label-width="formLabelWidth" prop="email">
            <el-input v-model="form.email"></el-input>
          </el-form-item>
          <el-form-item v-if="dialogType === 'create'" label="密码" :label-width="formLabelWidth" prop="password">
            <el-input v-model="form.password"></el-input>
          </el-form-item>
          <el-form-item label="角色" :label-width="formLabelWidth">
            <el-input v-model="form.role"></el-input>
          </el-form-item>

          <el-form-item label="权限组名称" :label-width="formLabelWidth">
            <el-select v-model="form.permission_group_id" placeholder="请选择">
              <el-option label="66" value="1"></el-option>
              <el-option label="123" value="2"></el-option>
              <el-option label="456" value="3"></el-option>
              <el-option label="890" value="4"></el-option>
            </el-select>
          </el-form-item>
           <el-form-item label="联系电话" :label-width="formLabelWidth">
            <el-input v-model="form.phone"></el-input>
          </el-form-item >
            <el-form-item label="介绍">
            <el-input type="textarea" v-model="form.intro"></el-input>
          </el-form-item>
        </el-form>

        <div slot="footer" class="dialog-footer">
          <el-button @click="cancelBtn">取 消</el-button>
          <el-button type="primary" @click="addForm">确 定</el-button>
        </div>
      </el-dialog>
    </el-form>

    <!-- 中间分隔 -->
    <div class="m_alert">
      <i class="el-icon-info"></i>
      <div class="m_text">
        共<span>{{ tableData.length }}</span
        >条数据
      </div>
    </div>

    <!-- table表单 -->
    <template>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="id" label="序号" width="114"> </el-table-column>
        <el-table-column prop="email" label="邮箱" width="114">
        </el-table-column>
        <el-table-column prop="phone" label="联系电话" width="179">
        </el-table-column>
        <el-table-column prop="username" label="用户名" width="179">
        </el-table-column>
        <el-table-column
          prop="permission_group_title"
          label="权限组名称"
          width="179"
        >
        </el-table-column>
        <el-table-column prop="role" label="角色" width="169">
        </el-table-column>

        <!--右侧标签 默认插槽 -->
        <el-table-column label="操作" width="90">
          <template #default="{ row }">
            <div class="actions">
              <i
                class="el-icon-edit el-icon"
                @click="editBtn('edit',row.id)"
              ></i>
              <i class="el-icon-delete el-icon" @click="del(row.id)"></i>
            </div>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页导航 -->
      <div class="block">
        <el-pagination
          background
          :current-page="1"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="100"
          :total="total"
          layout=" prev, pager, next, sizes, jumper">
        </el-pagination>
      </div>

    </template>
  </div>
</template>

<script>
import { list, add, remove, update, detail } from '@/api/base/users.js'
export default {

  data () {
    return {
      id: null,
      total: 0,
      dialogType: 'create',
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请填写邮箱', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请填写密码', trigger: 'blur' }
        ]
      },

      dialogTableVisible: false,
      dialogFormVisible: false,
      form: {
        username: '',
        email: '',
        password: '',
        role: '',
        permission_group_id: 1,
        phone: '',
        avatar: ''
      },
      formLabelWidth: '100px',

      data: {
        page: 1,
        pagesize: 10
      },
      formInline: {
        userInput: '',
        region: ''
      },
      tableData: []
    }
  },

  created () {
    this.renderList()
  },

  methods: {
    // dialog 新增用户的功能
    dialogBtn (type) {
      this.dialogType = type
      // if (this.dialogType === 'create') {
      this.dialogFormVisible = true
      // 加个开关 通过type判断 v-if把编辑里的不展示的隐藏
    },
    cancelBtn () {
      this.dialogType = 'create'
      this.dialogFormVisible = false
    },

    // 表单点击确认
    async addForm () {
      try {
        if (this.dialogType === 'create') {
          const data = await add(this.form)
          console.log(data)
        }
        if (this.dialogType === 'edit') {
          await update({ id: this.id, ...this.form })
          this.renderList()

          // console.log(res)
        }
        this.form.username = ''
        this.form.email = ''
        this.form.password = ''
        this.form.role = ''
        this.form.permission_group_id = 1
        this.form.phone = ''
        this.form.avatar = ''
        this.dialogFormVisible = false
        this.$message.success(
          this.dialogType === 'create' ? '添加成功' : '编辑成功'
        )
        this.renderList()
      } catch (error) {
        this.$message.error('添加失败')
      }
    },

    // 渲染页面
    async  renderList () {
      const { data } = await list(this.data)
      this.tableData = data.list
      this.total = data.counts
    },
    onClick () {
      console.log('清空')
    },
    onSubmit () {
      console.log('submit!')
    },
    handleSizeChange (val) {
      this.pagesize = val
      // console.log(`每页 ${val} 条`)
    },
    handleCurrentChange (val) {
      this.page = val
      // console.log(`当前页: ${val}`)
    },

    // 修改按钮
    async editBtn (type, id) {
      this.id = id
      this.dialogType = type
      const { data } = await detail(id)
      // console.log(data)
      this.form.username = data.username
      this.form.email = data.email
      this.form.role = data.role
      this.form.permission_group_id = 1
      this.form.phone = data.phone
      this.form.avatar = data.avatar
      this.dialogFormVisible = true
    },

    // 删除
    async del (id) {
      try {
        const data = await remove(id)
        this.$message.success('删除成功')
        this.renderList()
        console.log(data)
      } catch (error) {
        this.$message.error('删除失败')
      }
    },

    // 清空搜索框
    inputClick () {
      this.formInline.userInput = ''
      this.renderList()
    },
    // 根据用户名筛选
    onSerch () {
      if (!this.formInline.userInput) return this.$message.error('不能为空')
      const data = this.tableData.filter((item) => {
        return item.username === this.formInline.userInput
      })
      this.tableData = data
    }
  },
  computed: {
    dialogTitle () {
      if (this.dialogType === 'create') return '添加用户'
      if (this.dialogType === 'edit') return '修改用户'
      else {
        return '添加用户'
      }
    }
  }
}
</script>

<style scoped lang="less">
.user_box {
  width: 1178px;
  // height: 417.5px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  border: 1px solid #ebeef5;
  background-color: #fff;
  color: #303133;
  transition: 0.3s;
  border-radius: 4px;
  overflow: hidden;
  margin: 20px;
  padding: 20px;
}
.addBtn{
    float: right;
    margin-left: 10px;
    color: #fff;
    background-color: #67c23a;
    border-color: #67c23a;
    border: 1px solid #dcdfe6;
    font-weight: 500;
  }
.m_alert {
  display: flex;
  background-color: #f4f4f5;
  color: #909399;
  width: 100%;
  padding: 8px 16px;
  margin: 0;
  border-radius: 4px;
  color: #909399;
  i {
    display: inline-block;
    font-size: 16px;
    width: 16px;
    line-height: 1;
    margin-right: 5px;
  }
  .m-text {
    font-size: 13px;
    line-height: 18px;
  }

}
.el-pagination {
  margin-top: 20px;
  text-align: right;
}

.actions {
  width: 60px;
  height: 36px;
  margin-right: 5px;
  // padding-left: 5px;
  // padding-right: 5px;
  .el-icon {
    width: 14px;
    height: 14px;
    font-size: 14px;
    padding: 10px;

    line-height: 1;
    border-radius: 50%;
    display: inline-block;
    line-height: 1;
    white-space: nowrap;
    cursor: pointer;
    border: 1px solid #dcdfe6;
    transition: 0.1s;
    font-weight: 500;
  }
  .el-icon-edit {
    margin: 0 5px;
    margin-top: 6px;
    color: #409eff;
    background: #ecf5ff;
    border-color: #b3d8ff;
  }
  .el-icon-delete {
    margin-right: 5px;
    color: #f56c6c;
    background: #fef0f0;
    border-color: #fbc4c4;
  }
}
</style>
